/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.example;

import com.sun.star.beans.XPropertySet;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.container.XEnumeration;
import com.sun.star.container.XEnumerationAccess;
import com.sun.star.frame.XDesktop;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.matuschek.examples.LinkSelectWindow;

/**
 *
 * @author user
 */
public class LinkReader extends Thread{

    private Vector urlList;

    public void intialization() {
        this.start();

    }


    @Override
    public void run() {

        urlList = new Vector();
        System.out.println("initialized!! (before try catch)");

        try {
            com.sun.star.uno.XComponentContext m_xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
            XMultiComponentFactory xmcf = m_xContext.getServiceManager();
            Object desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
            XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop);
            XComponent xComponent = xDesktop.getCurrentComponent();
            //Reference: http://www.oooforum.org/forum/viewtopic.phtml?t=91713&start=0&postdays=0&postorder=asc&highlight=
            XTextDocument textDoc = (XTextDocument) UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, xComponent);
            System.out.println(textDoc.toString());


            // Get the paragraphs - The service 'com.sun.star.text.Text' supports the XEnumerationAccess interface
            XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, textDoc.getText());
            XEnumeration xParaEnum = xParaAccess.createEnumeration();
            while (xParaEnum.hasMoreElements()) {
                Object nextp = (Object) xParaEnum.nextElement();
                XTextContent xTextElement = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, nextp);


                // create another enumeration to get all text portions of the paragraph
                XEnumerationAccess xParaEnumerationAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, xTextElement);
                if (xParaEnumerationAccess != null) {
                    XEnumeration xTextPortionEnum = xParaEnumerationAccess.createEnumeration();
                    while (xTextPortionEnum.hasMoreElements()) {
                        XTextRange xTextPortion = (XTextRange) UnoRuntime.queryInterface(XTextRange.class, xTextPortionEnum.nextElement());

                        XPropertySet xSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextPortion);
                        String url = (String) xSet.getPropertyValue("HyperLinkURL");

                        if (!url.equalsIgnoreCase("")) { // found the text portion containing the HyperLink
                            System.out.println("Found URL ...  The URL is, " + url);
                            urlList.add(url);

                            // Fetch the Property Set, and Change the HyperLinkUrl property
                            XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(XTextRange.class, xTextPortion);
                            XTextCursor xTextCursor = xTextRange.getText().createTextCursorByRange(xTextRange);
                            XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
                            XTextContent textContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xTextPortion);

                        } else {
                            System.out.println("no url...!!!");
                        }
                    }
                }

            }
        } catch (Exception ex) {
            Logger.getLogger(LinkReader.class.getName()).log(Level.SEVERE, null, ex);

        } catch (BootstrapException ex) {
            Logger.getLogger(LinkReader.class.getName()).log(Level.SEVERE, null, ex);
        }

        LinkSelectWindow LSS2 = new LinkSelectWindow(this);

    }

    public void ThereadAlternative_RunMethod(){
        urlList = new Vector();
        System.out.println("initialized!! (before try catch)");

        try {
            com.sun.star.uno.XComponentContext m_xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
            XMultiComponentFactory xmcf = m_xContext.getServiceManager();
            Object desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
            XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop);
            XComponent xComponent = xDesktop.getCurrentComponent();
            //Reference: http://www.oooforum.org/forum/viewtopic.phtml?t=91713&start=0&postdays=0&postorder=asc&highlight=
            XTextDocument textDoc = (XTextDocument) UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, xComponent);
            System.out.println(textDoc.toString());


            // Get the paragraphs - The service 'com.sun.star.text.Text' supports the XEnumerationAccess interface
            XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, textDoc.getText());
            XEnumeration xParaEnum = xParaAccess.createEnumeration();
            while (xParaEnum.hasMoreElements()) {
                Object nextp = (Object) xParaEnum.nextElement();
                XTextContent xTextElement = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, nextp);


                // create another enumeration to get all text portions of the paragraph
                XEnumerationAccess xParaEnumerationAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, xTextElement);
                if (xParaEnumerationAccess != null) {
                    XEnumeration xTextPortionEnum = xParaEnumerationAccess.createEnumeration();
                    while (xTextPortionEnum.hasMoreElements()) {
                        XTextRange xTextPortion = (XTextRange) UnoRuntime.queryInterface(XTextRange.class, xTextPortionEnum.nextElement());

                        XPropertySet xSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextPortion);
                        String url = (String) xSet.getPropertyValue("HyperLinkURL");

                        if (!url.equalsIgnoreCase("")) { // found the text portion containing the HyperLink
                            System.out.println("Found URL ...  The URL is, " + url);
                            urlList.add(url);

                            // Fetch the Property Set, and Change the HyperLinkUrl property
                            XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(XTextRange.class, xTextPortion);
                            XTextCursor xTextCursor = xTextRange.getText().createTextCursorByRange(xTextRange);
                            XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
                            XTextContent textContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xTextPortion);

                        } else {
                            System.out.println("no url...!!!");
                        }
                    }
                }

            }
        } catch (Exception ex) {
            Logger.getLogger(LinkReader.class.getName()).log(Level.SEVERE, null, ex);

        } catch (BootstrapException ex) {
            Logger.getLogger(LinkReader.class.getName()).log(Level.SEVERE, null, ex);
        }

        LinkSelectWindow LSS2 = new LinkSelectWindow(this);
    }

    public Vector getUrlList() {
        return urlList;
    }
}
